package com.wan37.logic.handler.message;

import com.wan37.logic.backpack.item.Item;
import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.Result;
import com.wan37.logic.domain.vo.ItemVO;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.SceneServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.CommonBackpack;
import com.wan37.protobuf.message.client2server.game.GameBackpack;

import java.util.Map;

/**
 * @author : luoyong
 * @date : 2020-06-17 15:53
 **/
public class CGameItemHandler extends MsgHandler {
    private RoleServiceCache roleServiceCache;
    private RouteUtils routeUtils;
    private Item item;
    private SceneServiceCache sceneServiceCache;
    private UserServiceCache userServiceCache;

    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGameItemHandler(MessageVO messageVO) {
        super(messageVO);
        this.roleServiceCache = SpringContextHolder.getBean("roleServiceCache");
        this.routeUtils = SpringContextHolder.getBean("routeUtils");
        this.sceneServiceCache = SpringContextHolder.getBean("sceneServiceCache");
        this.userServiceCache = SpringContextHolder.getBean("userServiceCache");
    }

    @Override
    public void execute(Worker worker) throws Exception {
        GameBackpack.CGameItemMsg cGameItemMsg = (GameBackpack.CGameItemMsg) messageVO.getMessage();
        UserServiceCache.User user = userServiceCache.getUserServiceMap().get(messageVO.getUserId());
        Map<String, ItemVO> itemVOMap = user.getItemVOMap();
        int code = cGameItemMsg.getCode();
        switch (code) {
            case CommonBackpack.GET_ITEM:
                getItem(itemVOMap);
                break;
            case CommonBackpack.UPDATE_ITEM_REDUCE_COUNT:
                RoleServiceCache.Role role = roleServiceCache.getRoleServiceMap().get(cGameItemMsg.getRoleId());
                this.item = SpringContextHolder.getBean(user.getItemVOMap().get(cGameItemMsg.getBackpackId()).getEnglishName());
                Result result = item.execute(user, role, cGameItemMsg);
                if (result != null) {
                    routeUtils.sendItemResponse(result.getCode(),
                            result.getContent(), itemVOMap, messageVO);
                    //广播自己使用了物品，范围当前场景用户
                    routeUtils.sendItemBroadcastResponse(CommonBackpack.UPDATE_ITEM_REDUCE_COUNT,
                            sceneServiceCache.getSceneServiceMap().get(user.getSceneId()).getUserIdSet(),
                            role.getRoleVO().getAllRoleId(), user.getUserVO().getUserId(), cGameItemMsg.getBackpackId());
                }
                break;
            default:
                break;
        }
    }

    public void getItem(Map<String, ItemVO> itemVOMap) {
        routeUtils.sendItemResponse(CommonBackpack.ITEM_SUCCESS,
                messageVO.getUserId() + "物品信息获取成功！", itemVOMap, messageVO);
    }
}
